﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Base.Entity;
using T.ServiceComponent.DAC.Service;
using System.Reflection;
using System.Data;

namespace T.FAS.Runtime.Base.Dao
{
    public class ValidateRuleDao : DaoService<ValidateRule>
    {
        public override string TableName => "FAS_ValidateRule";
        public override string IDColumnName => "ID";
        public override List<string> ExceptColumnName => new List<string> { };

        public ValidateRuleDao() : base()
        {
            DBName = "TFAS";
        }

        public override Dictionary<string, object> GetDataMapping(ValidateRule data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove(nameof(ValidateRule.Type));
            datas.Add(nameof(ValidateRule.Type), (int)(data.Type));
            return datas;
        }

        public override ValidateRule ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove(nameof(ValidateRule.Type));
            var data = base.ConvertToObject(myCol, row);
            if (row[nameof(ValidateRule.Type)] != DBNull.Value)
                data.Type = (ValidateRuleType)Enum.Parse(typeof(ValidateRuleType), Convert.ToString(row[nameof(ValidateRule.Type)]));
            return data;
        }

        public List<ValidateRule> GetRules(string projID = null)
        {
            var list = new List<ValidateRule>();
            string filter = " 1=1 ";
            if (!string.IsNullOrEmpty(projID))
            {
                filter = $" ProjectID='{projID}' ";
            }
            var data = GetObjectsByFilter(filter);
            data?.ForEach(d => { if (d is ValidateRule rule) list.Add(rule); });
            return list.OrderBy(v => v.Name).ToList();
        }

        public ValidateRule GetRule(string id)
        {
            return GetObject(id);
        }
         
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="rule"></param>
        public void SaveRule(ValidateRule rule)
        {
            try
            {
                base.Add(new List<object> { rule });
            }
            catch (Exception e)
            {
                throw new FasException("FAS-Base-ValidateRule-001", e.Message, e);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ruleID"></param>
        /// <exception cref="FasException"></exception>
        public void DeleteRule(string ruleID)
        {
            try
            {
                base.Delete(new List<string> { ruleID });
            }
            catch (Exception e)
            {
                throw new FasException("FAS-Base-ValidateRule-002", e.Message, e);
            }
        }

        /// <summary>
        /// 更新 
        /// </summary>
        /// <param name="sm"></param>
        public void UpdateRule(ValidateRule rule)
        {
            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                {
                    DeleteRule(rule.ID);
                    SaveRule(rule);
                }
            }
            catch (Exception e)
            {
                throw new FasException("FAS-Base-ValidateRule-003", e.Message, e);
            }
        }
    }
}
